package com.stud.common.mybatis;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stud.common.utils.ListSplitUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Stud
 * @title mybatis-plus二次封装父类
 * @desription 用于扩展
 * @data 2022-08-20 10:53
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    /**
     * 高效批量插入
     *
     * @param entityList 实体对象
     */
    @Override
    public int insertBatchFast(List<T> entityList) {
        return insertBatchFast(entityList, 500);
    }

    /**
     * 高效批量插入
     *
     * @param entityList 实体对象
     */
    @Override
    public int insertBatchFast(List<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return 0;
        }
        List<List<T>> lists = ListSplitUtil.spilt(entityList, 500);
        AtomicInteger result = new AtomicInteger();
        lists.forEach(list -> {
            int num = baseMapper.insertBatchFast(list);
            result.addAndGet(num);
        });
        return result.get();
    }

    /**
     * 根据 ID 修改,属性为空则赋值为空
     *
     * @param entity 实体对象
     */
    @Override
    public boolean updateFullById(T entity) {
        log.debug(JSON.toJSONString(entity));
        return baseMapper.updateFullById(entity);
    }
}
